package main

import (
	"context"
	"fmt"
	"sync"
	"time"
)

var wg sync.WaitGroup

func main() {
	wg.Add(1)

	ctx, cancelFunc := context.WithCancel(context.Background())

	go c(ctx)
	for i := 0; i < 10; i++ {
		time.Sleep(time.Second * 1)
		if i >= 3 {
			cancelFunc()
		}
	}
	wg.Wait()

}

func c(context context.Context) {
	defer wg.Done()
	go q(context)
	wg.Add(1)
	select {
	case <-context.Done():
		fmt.Println("c 被取消了")
		return
	}

}

func q(context context.Context) {

	defer wg.Done()

	select {
	case <-context.Done():
		fmt.Println("q 被取消了")
		return
	}

}
